//
//  NewsData.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "NewsData.h"

@implementation NewsData

+ (NewsData *)shareInstance
{
    // 利用static的特性 第一次调用这个方法时 将指针置为空
    static NewsData *newsData = nil;
    if (nil == newsData) {
        newsData = [[NewsData alloc]init];
    }
    return newsData;
}


//打开数据库
-(void)openDB
{
    // 产生Documents地址
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接一个数据库文件的地址
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"reduce.db"];
    
    // 检测给定的路径下是否有数据库文件 如果有的话 直接讲sqlite3 数据库指针指向它； 如果没有 就在该路径下创建一个数据库文件
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    NSLog(@"打开数据库的结果: %d, path: %@", result, dbPath);
}



// 关闭数据库
- (void)closeDB
{
    sqlite3_close(dbPoint);
}

// 创建reduce
- (BOOL)creatNewsTable
{
    // 1.数据库的替身 用于临时存储数据和操作
    sqlite3_stmt *stmt = nil;
    
    // 2.准备sql语句
    // 参数1: 数据库指针
    // 参数2: sql语句
    // 参数3: sqli语句的长度限制(-1 为不限制)
    // 参数4: 数据库的替身地址
    
    // 返回值: 检查sql语句是否正确
    
    int result = sqlite3_prepare(dbPoint, "create table News(image text, title text,  summaryInfo text, Myid text primary key)", -1, &stmt, NULL);
    
    // 3.对sql语句的检查结果做判断
    if (SQLITE_OK == result) {
        // 4.执行sql语句
        // 对执行结果判断 正确条件为 SQLITE_DONE
        if (SQLITE_DONE == sqlite3_step(stmt)) {
            // 5.把替身中的东西写到本地数据库 销毁替身
            sqlite3_finalize(stmt);
            return YES;
        }
    }
    // 销毁替身
    sqlite3_finalize(stmt);
    return NO;
    
}



// 插入
- (BOOL)insertObjectToNews:(NewModel *)newsModel
{
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, "insert into News values(?, ?, ?, ?)", -1, &stmt, NULL);
    
    sqlite3_bind_text(stmt, 1, [newsModel.image UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 2, [newsModel.title UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 3, [newsModel.summaryInfo UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 4, [newsModel.Myid UTF8String], -1, NULL);
    
    
    if (SQLITE_OK == result) {
        if (SQLITE_DONE == sqlite3_step(stmt)) {
            sqlite3_finalize(stmt);
            return YES;
        }
    }
    sqlite3_finalize(stmt);
    return NO;
}


// 查询出reduce表中的所有数据
- (NSMutableArray *)selectAllNews
{
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, "select *from News", -1, &stmt, NULL);
    
    if (SQLITE_OK == result) {
        NSMutableArray *arr = [NSMutableArray array];
        while (SQLITE_ROW == sqlite3_step(stmt)) {
            
            const unsigned char *image = sqlite3_column_text(stmt, 0);
            NSString *imageStr = [NSString stringWithUTF8String:(const char *)image];
            
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            
            const unsigned char *summaryInfo = sqlite3_column_text(stmt, 2);
            NSString *summaryInfoStr = [NSString stringWithUTF8String:(const char *)summaryInfo];
            
            const unsigned char *Myid = sqlite3_column_text(stmt, 3);
            NSString *MyidStr = [NSString stringWithFormat:@"%",(const char *)Myid];
            
            NewModel *newsModel = [[NewModel alloc] init];
            
            newsModel.image = imageStr;
            newsModel.title = titleStr;
            newsModel.summaryInfo = summaryInfoStr;
            newsModel.Myid = MyidStr;
            
            [arr addObject:newsModel];
            
            [newsModel release];
        }
        sqlite3_finalize(stmt);
        return arr;
    }
    
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
    
}

// 删除降价表中的信息

- (BOOL)deleteNews:(NewModel *)newsModel
{
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, "delete from News where Myid = ?", -1, &stmt, NULL);
    sqlite3_bind_text(stmt, 1, [newsModel.Myid UTF8String], -1, NULL);
    
    if (SQLITE_OK == result) {
        if (SQLITE_DONE == sqlite3_step(stmt)) {
            sqlite3_finalize(stmt);
            return YES;
        }
    }
    sqlite3_finalize(stmt);
    return NO;
}

@end
